<?php

/**
 *
 * @author yupoxiong<i@yupoxiong.com>
 */


namespace app\api\controller;

use app\common\model\Adjust;
use app\common\model\ApiLog;
use app\common\model\Channel;
use app\common\model\User;
use think\facade\Log;
use think\Request;

class RegController
{


    /**
     * 返回注册url
     */
    public function url(Request $request)
    {
        $usercode = $request->param('usercode');
        $appcode = $request->param('appcode');
        if (!isset($appcode) || !isset($usercode)) {
            return json(['success' => 0, 'value' => [], 'message' => '参数错误', 'errCode' => 400]);
        }
        Log::write('app-获取注册url，参数：' . json_encode($request->param()), 'info');
        $channel_arr =  Channel::where('number', $appcode)->find(); //判断包是否错误
        Log::write('app-获取注册url，channel包信息：' . json_encode($channel_arr), 'info');
        if (!$channel_arr) {
            return  json(['success' => 0, 'value' => [], 'message' => '没有该渠道', 'errCode' => 400]);
        }
        $url = 'https://www.stock-biggroup.com/index/index/register?usercode=' . $usercode;
        $user_arr = User::where('usercode', $usercode)->find();
        if (!$user_arr) {
            User::create(['usercode' => $usercode, 'appcode' => $appcode]);
        }
        ApiLog::create([
            'code' => $usercode,
            'url' =>  $request->domain() . $request->url(),
            'result' => 'iframe页面地址',
            'create_time' => time(),
            'likai_time' => time()
        ]);

        Log::write('app-获取注册url，返回信息：' . $url, 'info');
        return json([
            'success' => 1,
            'value' => [
                'url' => $url,
            ],
            'message' => 'success',
            'errCode' => 200
        ]);
    }
    public function reg_url(Request $request)
    {
        $usercode = $request->param('usercode');
        $time = time();
        $user_arr = User::where('usercode', $usercode)->find();
        $url = 'https://www.hu777.store/reg?usercode=' . $usercode;
        $user_arr = User::where('usercode', $usercode)->find();
        if ($user_arr) {

            User::where('usercode', $usercode)->update(['update_time' => time()]);
            if ($user_arr['adid'] != '') {
                $adjust_arr = Adjust::where('adid', $user_arr['adid'])->find();
                if (isset($adjust_arr['tracker_name'])) { //是否有归因数据

                    $url = 'https://www.stock-biggroup.com/api/index/wjdc?usercode=' . $usercode;
                }
            }
        }
        ApiLog::create([
            'code' => $usercode,
            'url' =>  $request->domain() . $request->url(),
            'result' => $url,
            'create_time' => $time,
            'likai_time' => time()
        ]);
        return json([
            'success' => 1,
            'value' => [
                'url' => $url,
            ],
            'message' => 'success',
            'errCode' => 200
        ]);
    }
}
